package com.smedi.ismedi.dangeroussourcemanage.core.application.query.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.dangeroussourcemanage.core.application.command.QueryDangerousSourceLibraryCommand;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.DangerousSourceLibraryQueryService;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerousSourceLibraryVo;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.DangerousSourceLibrary;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.QueryDangerousSourceLibraryList;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.repository.DangerousSourceLibraryRepository;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 危险源库查询实现类
 *
 * @author xienannan
 * @date 2021/08/23
 */
@Service
@AllArgsConstructor
public class DangerousSourceLibraryQueryServiceImpl implements DangerousSourceLibraryQueryService {
    private final DangerousSourceLibraryRepository dangerousSourceLibraryRepository;

    private final DozerUtil dozerUtil;

    @Override
    public Page<DangerousSourceLibraryVo> postQueryDangerousSourceLibraryPageList(QueryDangerousSourceLibraryCommand queryDangerousSourceLibraryCommand) {
        Page<DangerousSourceLibraryVo> dangerousSourceLibraryVoPage = new Page<DangerousSourceLibraryVo>();
        QueryDangerousSourceLibraryList queryDangerousSourceLibraryList = dozerUtil.convertor(queryDangerousSourceLibraryCommand, QueryDangerousSourceLibraryList.class);
        Page<DangerousSourceLibrary> dangerousSourceLibraryPage = dangerousSourceLibraryRepository.selectDangerousSourceLibraryPageList(queryDangerousSourceLibraryList);
        if (dangerousSourceLibraryPage == null) {
            return null;
        }

        List<DangerousSourceLibraryVo> dangerousSourceLibraryVoList = dozerUtil.convertor(dangerousSourceLibraryPage.getRecords(), DangerousSourceLibraryVo.class);

        dangerousSourceLibraryVoPage.setRecords(dangerousSourceLibraryVoList);
        dangerousSourceLibraryVoPage.setTotal(dangerousSourceLibraryPage.getTotal());

        return dangerousSourceLibraryVoPage;
    }

    @Override
    public List<DangerousSourceLibraryVo> postQueryDangerousSourceLibraryList(QueryDangerousSourceLibraryCommand queryDangerousSourceLibraryCommand) {
        QueryDangerousSourceLibraryList queryDangerousSourceLibraryList = dozerUtil.convertor(queryDangerousSourceLibraryCommand, QueryDangerousSourceLibraryList.class);

        List<DangerousSourceLibrary>  dangerousSourceLibraryList = dangerousSourceLibraryRepository.selectDangerousSourceLibraryList(queryDangerousSourceLibraryList);
        if (dangerousSourceLibraryList == null) {
            return null;
        }

        List<DangerousSourceLibraryVo> dangerousSourceLibraryVoList = dozerUtil.convertor(dangerousSourceLibraryList, DangerousSourceLibraryVo.class);

        return dangerousSourceLibraryVoList;
    }

}
